Use Generics For Patterns

This evening I made a fun discovery in Java which I've never read about before (though surely it's been done, and was probably an intended use). It involves using generics and interface inheritance to ease (and enforce) the use of patterns.

Create a generic interface:

public interface DAO <T,IdType> {
public IdType createOrUpdate( T object );
public List<T> list();
public T get( IdType id );
public void delete( IdType id );
}


Create a business intrface derived from the generic:

public interface EventFacade extends DAO <Event,Integer> {
...
}


Implement the business object derived from the interface:

public class EventFacadeBean implements EventFacade {

public Integer createOrUpdate(Event event) {
// TODO Auto-generated method stub
return null;
}

public Event get(Integer id) {
// TODO Auto-generated method stub
return null;
}

public List list() {
// TODO Auto-generated method stub
return null;
}

public void delete(Integer id) {
// TODO Auto-generated method stub
return false;
}

...
}


As you can see the DAO interface is no longer generic in the concrete implementation of the class. This makes maitenece a LOT less tedious and definitely helps with implementing "patterns."